
/*-----------------------------------------------------------------------------*
* REPLICATION for "Do Provinces that Win the Spanish Christmas Lottery 
Experience a Surge in  Incumbent Popularity? An Out-of-Sample Replication of 
Bagues and Esteve-Volart (2016)". JPE Micro
* Authors: Carolina Bernal, Donald Green, and David Vilalta

* What for: Code for Individual Support for the Incumbent Party from Survey 
Data -- Tables 3 and 4. Main paper
* Do file's author: Carolina Bernal
**----------------------------------------------------------------------------*/

cls

global folder "/Users/carolinabernal/Library/CloudStorage/Dropbox/Spanish_Lottery/StataDos_RScripts/202406 Analysis/Ours_replication_pkg/NEW AND FINAL REPPKG"

cd "${folder}"


* Path to figures and tables folders: 
global tables "Results/Tables_Survey/"
global figures "Results/Figures_Survey/" 

**----------------------------------------------------------------------------**
*** TABLE 3. MAIN PAPER: Effects of Last Year's Lottery Prizes on Survey 
*** Measures of Incumbent Party Support, by Period
**----------------------------------------------------------------------------**

use "Data/Our_data/20240723_SpanishLottery_Complete_survey.dta", clear 

drop if province==""
encode province, gen(prov_num)

decode fixed_effect_num, gen(fixed)

** Get period with 3 quarters from year t-1 + first quarter of year t. 
rename year year_original
split fixed, parse(-) limit(1) gen(year)
destring year1, replace

** Period control:
gen preperiod=1 if year_original<2010
replace preperiod=0 if year_original>=2010

global individual_characteristics "i.municipality_size female age i.education i.status"
global ind_char_preperiod "preperiod#i.municipality_size preperiod#female preperiod#c.age preperiod#i.education preperiod#i.status"


preserve

keep if month<4

** Col 4, New Table 3. Propensities corrected + Fixed effects corrected, PRE period, Q1: 
eststo b_preQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num $individual_characteristics if year_original<2010, absorb(survey) 

estadd local SurveyFE "$\checkmark$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\times$"
estadd local PeriodCtrols "$\times$"
estadd local Data "Ours"
estadd local Sample "Pre-10"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "Q1"

boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)


** Col 5, New Table 3. Propensities corrected + Fixed effects corrected, POST period, Q1:
eststo b_postQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num $individual_characteristics if year_original>=2010, absorb(survey) 

estadd local SurveyFE "$\checkmark$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\times$"
estadd local PeriodCtrols "$\times$"
estadd local Data "Ours"
estadd local Sample "Post-09"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "Q1"

boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)

** Col 6, New Table 3. Propensities corrected + Fixed effects corrected, POST period, Q1:
eststo b_pooledQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num preperiod $individual_characteristics $ind_char_preperiod i.prov_num#preperiod, absorb(survey) 

estadd local SurveyFE "$\checkmark$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\checkmark$"
estadd local PeriodCtrols "$\checkmark$"
estadd local Data "Ours"
estadd local Sample "Pooled"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "Q1"

boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)


restore 

*** Assemble TABLE 3. MAIN PAPER 

local suffix: display %tdCCYYNNDD =daily("`c(current_date)'", "DMY")	

esttab b_preQ1 b_postQ1 b_pooledQ1 using ${tables}`suffix'_Survey_Table_3_periodCtrol.tex, ///
		keep(top_prizes_gdp_ours) nocon r2 nostar ///		
		cells(b(fmt(3)) se(fmt(3) par) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
		mtitles se mtitles("Vote Incumb." "Vote Incumb." ///
		"Vote Incumb.")  ///
		coeflabels(top_prizes_gdp_ours "Top Lottery prizes" expenditure_gdp_ours "Lottery expenditure") ///
		scalars("Estimation Estimation" "Data Data" "Sample Sample" "Outcome Time Outcome" "ProvinceFE Province FE" "YearFE Year FE" "SurveyFE Survey FE" "Propensity Propensity corrected")  replace				
		
		
/* NOTE: The WCB confidence sets are manually added to the tables from the Stata output 
generated here.
*/


**----------------------------------------------------------------------------**
*** TABLE 4. MAIN PAPER: Effects of Lottery Prizes Cumulated over Electoral Term 
*** on Survey Measures of Incumbent Party Support, by Period and Survey Date
**----------------------------------------------------------------------------**


use "Data/Our_data/20240723_SpanishLottery_Complete_survey.dta", clear 

drop if province==""
encode province, gen(prov_num)

decode fixed_effect_num, gen(fixed)

** Month (for which we have survery data) before the election: 
gen preelectoral = 1 if yearmonth==tm(1986m5) | yearmonth==tm(1989m9) | ///
yearmonth==tm(1993m5) | yearmonth==tm(1996m2) | yearmonth==tm(2000m2) | ///
yearmonth==tm(2004m1) | yearmonth==tm(2008m1) | yearmonth==tm(2011m7) | ///
yearmonth==tm(2015m10) | yearmonth==tm(2016m4)| yearmonth==tm(2019m3) 
** Merge based on yearmonth province. 

preserve

***

* read electoral data
use "Data/Our_data/20250531_SpanishLottery_Complete_province.dta", clear 

*gen yearmonth=ym(year, month)
replace yearmonth = yearmonth
format yearmonth %tm
label var yearmonth "Year-Month"

/*This is to match month of election in electoral data to pre month in survey 
data. In any case, what I'm merging is the cumulative prize data */
replace yearmonth = tm(1986m5) if yearmonth == tm(1986m6)
replace yearmonth = tm(1989m9) if yearmonth == tm(1989m10)
replace yearmonth = tm(1993m5) if yearmonth == tm(1993m6)
replace yearmonth = tm(1996m2) if yearmonth == tm(1996m3)
replace yearmonth = tm(2000m2) if yearmonth == tm(2000m3)
replace yearmonth = tm(2004m1) if yearmonth == tm(2004m3)
replace yearmonth = tm(2008m1) if yearmonth == tm(2008m3)
replace yearmonth = tm(2011m7) if yearmonth == tm(2011m11)
replace yearmonth = tm(2015m10) if yearmonth == tm(2015m12)
replace yearmonth = tm(2016m4) if yearmonth == tm(2016m6)
replace yearmonth = tm(2019m3) if yearmonth == tm(2019m4)


keep top_prizes_gdp_term_2 expenditure_gdp_term_2 year month yearmonth province_num province 


** province var is already clean: lower cased.

tempfile electoral_mon

save `electoral_mon'

restore /*Load survey data back*/

preserve

***

** Keep only observations that correspond to the (available) month before election.
keep if preelectoral==1

merge m:1 yearmonth province using `electoral_mon'
drop if _merge!=3 /*1986, 2019-11, 2023 didn't merge b/c no survey data for them */

** Get period with 3 quarters from year t-1 + first quarter of year t. 
rename year year_original
split fixed, parse(-) limit(1) gen(year)
destring year1, replace

** Period control:
gen preperiod=1 if year_original<2010
replace preperiod=0 if year_original>=2010


global individual_characteristics "i.municipality_size female age i.education i.status"
global ind_char_preperiod "preperiod#i.municipality_size preperiod#female preperiod#c.age preperiod#i.education preperiod#i.status"
*global ind_char_preperiod "i.year_original#i.municipality_size i.year_original#female i.year_original#c.age i.year_original#i.education i.year_original#i.status"

** Col 1, TABLE 4. Propensities corrected + Fixed effects corrected, PRE period, month preelection: 
eststo b_pre_m: eststo b_pre_m: areg vote_incumbent top_prizes_gdp_term_2 c.expenditure_gdp_term_2##i.year_original  $individual_characteristics if year_original<2010, absorb(prov_num) 

*areg vote_incumbent top_prizes_gdp_term_2 c.expenditure_gdp_term_2##i.year_original i.prov_num $individual_characteristics if year_original<2010, absorb(survey) 

estadd local SurveyFE "$\times$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\times$"
estadd local PeriodCtrols "$\times$"
estadd local Data "Ours"
estadd local Sample "Pre-10"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "Month pre-elect"

boottest top_prizes_gdp_term_2, cluster(prov_num) seed(12345)
gen pre_sample=1 if e(sample)==1


** Col 2, TABLE 4. Propensities corrected + Fixed effects corrected, POST period, month preelection:
eststo b_post_m: areg vote_incumbent top_prizes_gdp_term_2 c.expenditure_gdp_term_2##i.year_original $individual_characteristics if year_original>=2010, absorb(prov_num)

estadd local SurveyFE "$\times$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\times$"
estadd local PeriodCtrols "$\times$"
estadd local Data "Ours"
estadd local Sample "Post-09"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "Month pre-elect"

boottest top_prizes_gdp_term_2, cluster(prov_num) seed(12345)
gen post_sample=1 if e(sample)==1


** Col 3, TABLE 4. Propensities corrected + Fixed effects corrected, POOLED period, month preelection:
eststo b_pooled_m: reg vote_incumbent top_prizes_gdp_term_2 c.expenditure_gdp_term_2##i.year_original i.prov_num preperiod $individual_characteristics $ind_char_preperiod i.prov_num#preperiod

estadd local SurveyFE "$\times$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\checkmark$"
estadd local PeriodCtrols "$\checkmark$"
estadd local Data "Ours"
estadd local Sample "Pooled"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "Month pre-elect"

boottest top_prizes_gdp_term_2, cluster(prov_num) seed(12345)

gen pooled_sample=1 if e(sample)==1
cou if pooled_sample==1 & pre_sample==. & post_sample==.
cou if pooled_sample==1 & (pre_sample==1 | post_sample==1)

drop *_sample

restore /* Going back to version of survey data that doenst have cumulative prize added yet*/


preserve  

*****

* read electoral data
use "Data/Our_data/20250531_SpanishLottery_Complete_province.dta", clear 


****

rename year year1

keep province year1 top_prizes_gdp_term_2 expenditure_gdp_term_2
duplicates drop /*To drop the second 2019 election since prizes are the same */


tempfile electoral_yearQ

save `electoral_yearQ'


restore /* Re load survey data */


** Get period with 3 quarters from year t-1 + first quarter of year t. 
rename year year_original
split fixed, parse(-) limit(1) gen(year)
destring year1, replace

** Period control:
gen preperiod=1 if year_original<2010
replace preperiod=0 if year_original>=2010

* merge
merge m:1 year1 province using `electoral_yearQ' 
keep if _merge==3 
drop _merge


* replace with zeroes 
replace top_prizes_gdp_term_2=0 if month>3
replace expenditure_gdp_term_2=0 if month>3

drop if month>=4


** Col 4, TABLE 4. Propensities corrected + Fixed effects corrected, PRE period, Q1: 
eststo b_pre_Q1: areg vote_incumbent top_prizes_gdp_term_2 c.expenditure_gdp_term_2##i.year_original i.prov_num $individual_characteristics if year_original<2010, absorb(survey)

estadd local SurveyFE "$\checkmark$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\times$"
estadd local PeriodCtrols "\times$"
estadd local Data "Ours"
estadd local Sample "Pre-10"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "Q1"

boottest top_prizes_gdp_term_2, cluster(prov_num) seed(12345)
gen pre_sample=1 if e(sample)==1

** Col 5, TABLE 4. Propensities corrected + Fixed effects corrected, POST period, Q1:
eststo b_post_Q1: areg vote_incumbent top_prizes_gdp_term_2 c.expenditure_gdp_term_2##i.year_original i.prov_num $individual_characteristics if year_original>=2010, absorb(survey) 

estadd local SurveyFE "$\checkmark$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\times$"
estadd local PeriodCtrols "\times$"
estadd local Data "Ours"
estadd local Sample "Post-09"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "Q1"

boottest top_prizes_gdp_term_2, cluster(prov_num) seed(12345)
gen post_sample=1 if e(sample)==1


** Col 6, TABLE 4. Propensities corrected + Fixed effects corrected, POOLED period, Q1:
eststo b_pooled_Q1: areg vote_incumbent top_prizes_gdp_term_2 c.expenditure_gdp_term_2##i.year_original i.prov_num preperiod $individual_characteristics $ind_char_preperiod i.prov_num#preperiod, absorb(survey) 

estadd local SurveyFE "$\checkmark$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\checkmark$"
estadd local PeriodCtrols "$\checkmark$"
estadd local Data "Ours"
estadd local Sample "Pooled"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "Q1"

boottest top_prizes_gdp_term_2, cluster(prov_num) seed(12345)
gen pooled_sample=1 if e(sample)==1

cou if pooled_sample==1 & pre_sample==. & post_sample==.
cou if pooled_sample==1 & (pre_sample==1 | post_sample==1)

drop *_sample

*****

*** Assemble TABLE 4. MAIN PAPER 

local suffix: display %tdCCYYNNDD =daily("`c(current_date)'", "DMY")	

esttab b_pre_m b_post_m b_pooled_m b_pre_Q1 b_post_Q1 b_pooled_Q1 using ${tables}`suffix'_Survey_Table_4_periodCtrol.tex, ///
		keep(top_prizes_gdp_term_2) nocon r2 nostar ///		
		cells(b(fmt(3)) se(fmt(3) par) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
		mtitles se mtitles("Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." ///
		"Vote Incumb.")  ///
		coeflabels(top_prizes_gdp_term_2 "Top Lottery prizes Term" expenditure_gdp_term_2 "Lottery expenditure Term") ///
		scalars("Estimation Estimation" "Data Data" "Sample Sample" "Outcome Time Outcome" "ProvinceFE Province FE" "YearFE Year FE" "PeriodFE Period FE" "PeriodCtrols Ctrols x Period" "SurveyFE Survey FE" "Propensity Propensity corrected")  replace		
		
/* NOTE: Information of the last column of the table (the meta-analysis) and 
WCB confidence sets are manually added to the tables from the Stata output 
generated here.
*/
